<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  
  <title>Page 2 | H5教案</title>

  
  <meta name="author" content="John Doe">
  

  

  

  <meta id="viewport" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">

  

  <meta property="og:site_name" content="H5教案"/>

  
  <meta property="og:image" content="/ericxie-book/favicon.ico"/>
  

  <link href="/ericxie-book/favicon.ico" rel="icon">
  <link rel="alternate" href="/ericxie-book/atom.xml" title="H5教案" type="application/atom+xml">
  <link rel="stylesheet" href="/ericxie-book/css/style.css" media="screen" type="text/css">
<meta name="generator" content="Hexo 5.3.0"></head>


<body>
<div class="blog">
  <div class="content">

    <header>
  <div class="site-branding">
    <h1 class="site-title">
      <a href="/ericxie-book/">H5教案</a>
    </h1>
    <p class="site-description"></p>
  </div>
  <nav class="site-navigation">
    <ul>
      
    </ul>
  </nav>
</header>

    <main class="site-main posts-loop">
    
  <article>

  
    
    <h3 class="article-title"><a href="/ericxie-book/2021/01/26/event/"><span>event</span></a></h3>
    
  

  <div class="article-top-meta">
    <span class="posted-on">
      <a href="/ericxie-book/2021/01/26/event/" rel="bookmark">
        <time class="entry-date published" datetime="2021-01-26T03:34:18.000Z">
          2021-01-26
        </time>
      </a>
    </span>
  </div>


  

  <div class="article-content">
    <div class="entry">
      
        <h2 id="5、事件"><a href="#5、事件" class="headerlink" title="5、事件"></a>5、事件</h2><p>模块是events</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">const EventEmitter=require(<span class="string">&quot;events&quot;</span>);</span><br></pre></td></tr></table></figure>
<h3 id="在Nodejs中事件的侦听者和事件对象是同一个，也就是说事件侦听对象自身就是一个事件"><a href="#在Nodejs中事件的侦听者和事件对象是同一个，也就是说事件侦听对象自身就是一个事件" class="headerlink" title="在Nodejs中事件的侦听者和事件对象是同一个，也就是说事件侦听对象自身就是一个事件"></a>在Nodejs中事件的侦听者和事件对象是同一个，也就是说事件侦听对象自身就是一个事件</h3><h4 id="直到node-14以后才新增了事件对象"><a href="#直到node-14以后才新增了事件对象" class="headerlink" title="直到node 14以后才新增了事件对象"></a>直到node 14以后才新增了事件对象</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">const EventEmitter=require(<span class="string">&quot;events&quot;</span>);</span><br><span class="line">var event=new EventEmitter();</span><br><span class="line">event.on(<span class="string">&quot;play&quot;</span>,<span class="keyword">function</span>(data)&#123;</span><br><span class="line">    console.log(data);</span><br><span class="line">&#125;);</span><br><span class="line">event.emit(<span class="string">&quot;play&quot;</span>);</span><br><span class="line">event.emit(<span class="string">&quot;play&quot;</span>,<span class="string">&quot;nihao&quot;</span>)</span><br></pre></td></tr></table></figure>
<h4 id="事件也可以继承"><a href="#事件也可以继承" class="headerlink" title="事件也可以继承"></a>事件也可以继承</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">const EventEmitter=require(<span class="string">&quot;events&quot;</span>);</span><br><span class="line">class MyEventEmitter extends EventEmitter&#123;&#125;</span><br><span class="line">var event=new MyEventEmitter();</span><br><span class="line"><span class="comment"># 仅处理一次事件侦听</span></span><br><span class="line">event.once(<span class="string">&quot;play&quot;</span>,<span class="keyword">function</span>(data)&#123;</span><br><span class="line">    console.log(data);</span><br><span class="line">&#125;);</span><br><span class="line">event.emit(<span class="string">&quot;play&quot;</span>);</span><br><span class="line">event.emit(<span class="string">&quot;play&quot;</span>,<span class="string">&quot;nihao&quot;</span>)</span><br></pre></td></tr></table></figure>
<h4 id="删除事件"><a href="#删除事件" class="headerlink" title="删除事件"></a>删除事件</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">const EventEmitter=require(<span class="string">&quot;events&quot;</span>);</span><br><span class="line">var event=new EventEmitter();</span><br><span class="line">event.on(<span class="string">&quot;play&quot;</span>,eventHandler);</span><br><span class="line">event.emit(<span class="string">&quot;play&quot;</span>);</span><br><span class="line">event.emit(<span class="string">&quot;play&quot;</span>,<span class="string">&quot;nihao&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> eventHandler(data)&#123;</span><br><span class="line">    event.off(<span class="string">&quot;play&quot;</span>,eventHandler);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>

  </div>

  <div class="article-footer">
    <div class="article-meta pull-left">

    

    

    </div>

    
  </div>
</article>



  <article>

  
    
    <h3 class="article-title"><a href="/ericxie-book/2021/01/26/WebCrawlers/"><span>WebCrawlers</span></a></h3>
    
  

  <div class="article-top-meta">
    <span class="posted-on">
      <a href="/ericxie-book/2021/01/26/WebCrawlers/" rel="bookmark">
        <time class="entry-date published" datetime="2021-01-26T01:49:21.000Z">
          2021-01-26
        </time>
      </a>
    </span>
  </div>


  

  <div class="article-content">
    <div class="entry">
      
        <h2 id="4、爬虫"><a href="#4、爬虫" class="headerlink" title="4、爬虫"></a>4、爬虫</h2><p>cheerio是模仿jQuery方式实现的一个第三方爬虫解析插件，可以将文本像html一样通过<br>选择器和筛选器将数据分离</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">var http=require(<span class="string">&quot;http&quot;</span>);</span><br><span class="line">var https=require(<span class="string">&quot;https&quot;</span>);</span><br><span class="line">var url=require(<span class="string">&quot;url&quot;</span>);</span><br><span class="line">var cheerio=require(<span class="string">&quot;cheerio&quot;</span>);</span><br><span class="line"><span class="keyword">function</span> createServer(fn)&#123;</span><br><span class="line">        http.createServer((req,res)=&gt;&#123;</span><br><span class="line">            res.writeHead(200,&#123;</span><br><span class="line">                <span class="string">&quot;content-type&quot;</span>:<span class="string">&quot;application/json;charset=utf-8&quot;</span>,</span><br><span class="line">                <span class="string">&quot;Access-Control-Allow-Origin&quot;</span>:<span class="string">&quot;*&quot;</span></span><br><span class="line">            &#125;)</span><br><span class="line">         <span class="built_in">return</span>  fn(&#123;req:req,res:res&#125;);</span><br><span class="line">        &#125;).listen(9000);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">function</span> createRequest(req,res)&#123;</span><br><span class="line">    <span class="built_in">return</span> new Promise((resolve,reject)=&gt;&#123;</span><br><span class="line">       var reqs=https.request(url.parse(<span class="string">&quot;https://maoyan.com:443/&quot;</span>),(reslute=&gt;&#123;</span><br><span class="line">            <span class="built_in">let</span> data=<span class="string">&quot;&quot;</span>;</span><br><span class="line">            reslute.on(<span class="string">&quot;data&quot;</span>,_data=&gt;data+=_data);</span><br><span class="line">            reslute.on(<span class="string">&quot;end&quot;</span>,<span class="function"><span class="title">function</span></span>()&#123;</span><br><span class="line">               resolve(&#123;req:req,res:res,data:data&#125;);</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;));</span><br><span class="line">        reqs.end();</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> filterData(data)&#123;</span><br><span class="line">    $=cheerio.load(data);</span><br><span class="line">    <span class="built_in">let</span> movieList=$(<span class="string">&quot;.movie-item&quot;</span>);</span><br><span class="line">    <span class="built_in">let</span> movies = []</span><br><span class="line">    movieList.each((index, value) =&gt; &#123;</span><br><span class="line">      movies.push(&#123;</span><br><span class="line">        title: $(value).find(<span class="string">&#x27;.movie-title&#x27;</span>).attr(<span class="string">&#x27;title&#x27;</span>),</span><br><span class="line">        score: $(value).find(<span class="string">&#x27;.movie-score i&#x27;</span>).text(),</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="built_in">return</span> movies;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">createServer(<span class="keyword">function</span>(&#123;req,res&#125;)&#123;</span><br><span class="line">  <span class="built_in">return</span>  createRequest(req,res);</span><br><span class="line">&#125;).<span class="keyword">then</span>(<span class="keyword">function</span>(&#123;req,res,data&#125;)&#123;</span><br><span class="line">    res.end(JSON.stringify(filterData(data)));</span><br><span class="line">&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
      
    </div>

  </div>

  <div class="article-footer">
    <div class="article-meta pull-left">

    

    

    </div>

    
  </div>
</article>



  <article>

  
    
    <h3 class="article-title"><a href="/ericxie-book/2021/01/25/http-https/"><span>http-https</span></a></h3>
    
  

  <div class="article-top-meta">
    <span class="posted-on">
      <a href="/ericxie-book/2021/01/25/http-https/" rel="bookmark">
        <time class="entry-date published" datetime="2021-01-25T12:00:31.000Z">
          2021-01-25
        </time>
      </a>
    </span>
  </div>


  

  <div class="article-content">
    <div class="entry">
      
        <h2 id="3、http和https"><a href="#3、http和https" class="headerlink" title="3、http和https"></a>3、http和https</h2><h3 id="3-1、get"><a href="#3-1、get" class="headerlink" title="3.1、get"></a>3.1、get</h3><p>由于大多数请求都是没有主体的 GET 请求,这里就写了一个简易的get请求<br>主要用于请求远程服务的文件.例如[<a target="_blank" rel="noopener" href="http://localhost:4001/server/config.json]">http://localhost:4001/server/config.json]</a></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">var http = require(<span class="string">&#x27;http&#x27;</span>)</span><br><span class="line">var https = require(<span class="string">&#x27;https&#x27;</span>)</span><br><span class="line"></span><br><span class="line">const server = http.createServer((request, response) =&gt; &#123;</span><br><span class="line">  var url = request.url.substr(1)</span><br><span class="line">  var data = <span class="string">&#x27;&#x27;</span></span><br><span class="line">  response.writeHeader(200, &#123;</span><br><span class="line">    <span class="string">&#x27;content-type&#x27;</span>: <span class="string">&#x27;application/json;charset=utf-8&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span>: <span class="string">&#x27;*&#x27;</span></span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">#   response.setEncoding(&quot;utf8&quot;);//或者这样设置utf-8</span></span><br><span class="line">  https.get(`https://m.lagou.com/listmore.json<span class="variable">$&#123;url&#125;</span>`, (res) =&gt; &#123;</span><br><span class="line"></span><br><span class="line">    res.on(<span class="string">&#x27;data&#x27;</span>, (chunk) =&gt; &#123;</span><br><span class="line">      data += chunk</span><br><span class="line">    &#125;)</span><br><span class="line"></span><br><span class="line">    res.on(<span class="string">&#x27;end&#x27;</span>, () =&gt; &#123;</span><br><span class="line">      response.end(JSON.stringify(&#123;</span><br><span class="line">        ret: <span class="literal">true</span>,</span><br><span class="line">        data</span><br><span class="line">      &#125;))</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">server.listen(8080, () =&gt; &#123;</span><br><span class="line">  console.log(<span class="string">&#x27;localhost:8080&#x27;</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="3-2-post：服务器提交"><a href="#3-2-post：服务器提交" class="headerlink" title="3.2 post：服务器提交"></a>3.2 post：服务器提交</h3><p>通过request不断请求服务器进行攻击</p>
<h4 id="a-js"><a href="#a-js" class="headerlink" title="a.js"></a>a.js</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">const http = require(<span class="string">&#x27;http&#x27;</span>)</span><br><span class="line">const querystring = require(<span class="string">&#x27;querystring&#x27;</span>)</span><br><span class="line"></span><br><span class="line">const postData = querystring.stringify(&#123;</span><br><span class="line">    province: <span class="string">&#x27;上海&#x27;</span>,</span><br><span class="line">    city: <span class="string">&#x27;上海&#x27;</span>,</span><br><span class="line">    district: <span class="string">&#x27;宝山区&#x27;</span>,</span><br><span class="line">    address: <span class="string">&#x27;同济支路199号智慧七立方3号楼2-4层&#x27;</span>,</span><br><span class="line">    latitude: 43.0,</span><br><span class="line">    longitude: 160.0,</span><br><span class="line">    message: <span class="string">&#x27;求购一条小鱼&#x27;</span>,</span><br><span class="line">    contact: <span class="string">&#x27;13666666&#x27;</span>,</span><br><span class="line">    <span class="built_in">type</span>: <span class="string">&#x27;sell&#x27;</span>,</span><br><span class="line">    time: 1571217561</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">const options = &#123;</span><br><span class="line">    protocol: <span class="string">&#x27;http:&#x27;</span>,</span><br><span class="line">    hostname: <span class="string">&#x27;localhost&#x27;</span>,</span><br><span class="line">    method: <span class="string">&#x27;POST&#x27;</span>,</span><br><span class="line">    port: 4010,</span><br><span class="line">    path: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">    headers: &#123;</span><br><span class="line">        <span class="string">&#x27;Content-Type&#x27;</span>: <span class="string">&#x27;application/x-www-form-urlencoded&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;Content-Length&#x27;</span>: Buffer.byteLength(postData)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">http.createServer((req, res) =&gt; &#123;</span><br><span class="line">    res.writeHead(200, &#123;</span><br><span class="line">        <span class="string">&quot;Access-Control-Allow-Origin&quot;</span>: <span class="string">&quot;*&quot;</span></span><br><span class="line">    &#125;)</span><br><span class="line">    var request = http.request(options,result=&gt;requsetHandler(result,res));</span><br><span class="line">    request.write(postData);</span><br><span class="line">    request.end();</span><br><span class="line">&#125;).listen(4011);</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> requsetHandler(result,res) &#123;</span><br><span class="line">    var data = <span class="string">&quot;&quot;</span>;</span><br><span class="line">    result.on(<span class="string">&quot;data&quot;</span>, _data =&gt; &#123;</span><br><span class="line">        data += _data;</span><br><span class="line">    &#125;);</span><br><span class="line">    result.on(<span class="string">&quot;end&quot;</span>, () =&gt; &#123;</span><br><span class="line">        res.end(data);</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="b-js"><a href="#b-js" class="headerlink" title="b.js"></a>b.js</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">var http=require(<span class="string">&quot;http&quot;</span>);</span><br><span class="line">http.createServer((req,res)=&gt;&#123;</span><br><span class="line">    var data=<span class="string">&quot;&quot;</span>;</span><br><span class="line">    req.on(<span class="string">&quot;data&quot;</span>,_data=&gt;&#123;</span><br><span class="line">        console.log(_data+<span class="string">&quot;&quot;</span>);</span><br><span class="line">        data+=_data;</span><br><span class="line">    &#125;)</span><br><span class="line">    req.on(<span class="string">&quot;end&quot;</span>,()=&gt;&#123;</span><br><span class="line">        console.log(data);</span><br><span class="line">        res.end(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;).listen(4010);</span><br></pre></td></tr></table></figure>
<h3 id="3-3-跨域：jsonp"><a href="#3-3-跨域：jsonp" class="headerlink" title="3.3 跨域：jsonp"></a>3.3 跨域：jsonp</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">const http = require(<span class="string">&#x27;http&#x27;</span>)</span><br><span class="line">const url = require(<span class="string">&#x27;url&#x27;</span>)</span><br><span class="line"></span><br><span class="line">const app = http.createServer((req, res) =&gt; &#123;</span><br><span class="line">  <span class="built_in">let</span> urlObj = url.parse(req.url, <span class="literal">true</span>)</span><br><span class="line"></span><br><span class="line">  switch (urlObj.pathname) &#123;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">&#x27;/api/user&#x27;</span>:</span><br><span class="line">      res.end(`<span class="variable">$&#123;urlObj.query.cb&#125;</span>(&#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;gp145&quot;</span>&#125;)`)</span><br><span class="line">      <span class="built_in">break</span></span><br><span class="line">    default:</span><br><span class="line">      res.end(<span class="string">&#x27;404.html&#x27;</span>)</span><br><span class="line">      <span class="built_in">break</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">app.listen(8080)</span><br></pre></td></tr></table></figure>
<h3 id="3-4-跨域：CORS"><a href="#3-4-跨域：CORS" class="headerlink" title="3.4 跨域：CORS"></a>3.4 跨域：CORS</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">const http = require(<span class="string">&#x27;http&#x27;</span>)</span><br><span class="line">const url = require(<span class="string">&#x27;url&#x27;</span>)</span><br><span class="line">const querystring = require(<span class="string">&#x27;querystring&#x27;</span>)</span><br><span class="line"></span><br><span class="line">const app = http.createServer((req, res) =&gt; &#123;</span><br><span class="line">  <span class="built_in">let</span> data = <span class="string">&#x27;&#x27;</span></span><br><span class="line">  <span class="built_in">let</span> urlObj = url.parse(req.url, <span class="literal">true</span>)</span><br><span class="line"></span><br><span class="line">  res.writeHead(200, &#123;</span><br><span class="line">    <span class="string">&#x27;content-type&#x27;</span>: <span class="string">&#x27;application/json;charset=utf-8&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span>: <span class="string">&#x27;*&#x27;</span></span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  req.on(<span class="string">&#x27;data&#x27;</span>, (chunk) =&gt; &#123;</span><br><span class="line">    data += chunk</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  req.on(<span class="string">&#x27;end&#x27;</span>, () =&gt; &#123;</span><br><span class="line">    responseResult(querystring.parse(data))</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  <span class="keyword">function</span> responseResult(data) &#123;</span><br><span class="line">    switch (urlObj.pathname) &#123;</span><br><span class="line">      <span class="keyword">case</span> <span class="string">&#x27;/api/login&#x27;</span>:</span><br><span class="line">        res.end(JSON.stringify(&#123;</span><br><span class="line">          message: data</span><br><span class="line">        &#125;))</span><br><span class="line">        <span class="built_in">break</span></span><br><span class="line">      default:</span><br><span class="line">        res.end(<span class="string">&#x27;404.&#x27;</span>)</span><br><span class="line">        <span class="built_in">break</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">app.listen(8080, () =&gt; &#123;</span><br><span class="line">  console.log(<span class="string">&#x27;localhost:8080&#x27;</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h3 id="3-5-跨域：middleware（http-proxy-middware）"><a href="#3-5-跨域：middleware（http-proxy-middware）" class="headerlink" title="3.5 跨域：middleware（http-proxy-middware）"></a>3.5 跨域：middleware（http-proxy-middware）</h3><p>中间层代理模块，通过代理服务跨域访问网站</p>
<p>下载middleware</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i http-proxy-middleware -D</span><br></pre></td></tr></table></figure>
<p>跨域方式</p>
<h4 id="c-js"><a href="#c-js" class="headerlink" title="c.js"></a>c.js</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">var http=require(<span class="string">&quot;http&quot;</span>);</span><br><span class="line"></span><br><span class="line">var &#123;createProxyMiddleware&#125;=require(<span class="string">&quot;http-proxy-middleware&quot;</span>);</span><br><span class="line"></span><br><span class="line">http.createServer((req,res)=&gt;&#123;</span><br><span class="line">    res.writeHead(200, &#123;</span><br><span class="line">        <span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span>: <span class="string">&#x27;*&#x27;</span></span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="keyword">if</span>(/^\/api/.<span class="built_in">test</span>(req.url))&#123;</span><br><span class="line">        var apiProxy=createProxyMiddleware(<span class="string">&quot;&quot;</span>,&#123;</span><br><span class="line">            target:<span class="string">&quot;http://localhost:4010&quot;</span>,</span><br><span class="line">            changeOrigin:<span class="literal">true</span>,//修改托管</span><br><span class="line">            pathRewrite:&#123;</span><br><span class="line">                <span class="string">&#x27;^/api&#x27;</span>:<span class="string">&quot;&quot;</span>//是否修改请求路径</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        apiProxy(req,res);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;).listen(4011);</span><br></pre></td></tr></table></figure>
<h4 id="d-js"><a href="#d-js" class="headerlink" title="d.js"></a>d.js</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">var http=require(<span class="string">&quot;http&quot;</span>);</span><br><span class="line">http.createServer((req,res)=&gt;&#123;</span><br><span class="line">    var data=<span class="string">&quot;&quot;</span>;</span><br><span class="line">    req.on(<span class="string">&quot;data&quot;</span>,_data=&gt;&#123;</span><br><span class="line">        console.log(_data+<span class="string">&quot;&quot;</span>);</span><br><span class="line">        data+=_data;</span><br><span class="line">    &#125;);</span><br><span class="line">    req.on(<span class="string">&quot;end&quot;</span>,()=&gt;&#123;</span><br><span class="line">        console.log(data);</span><br><span class="line">        res.end(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;).listen(4010);</span><br></pre></td></tr></table></figure>
      
    </div>

  </div>

  <div class="article-footer">
    <div class="article-meta pull-left">

    

    

    </div>

    
  </div>
</article>



  <article>

  
    
    <h3 class="article-title"><a href="/ericxie-book/2021/01/25/querystring/"><span>querystring</span></a></h3>
    
  

  <div class="article-top-meta">
    <span class="posted-on">
      <a href="/ericxie-book/2021/01/25/querystring/" rel="bookmark">
        <time class="entry-date published" datetime="2021-01-25T11:53:38.000Z">
          2021-01-25
        </time>
      </a>
    </span>
  </div>


  

  <div class="article-content">
    <div class="entry">
      
        <h2 id="2、querystring"><a href="#2、querystring" class="headerlink" title="2、querystring"></a>2、querystring</h2><h3 id="2-1-parse"><a href="#2-1-parse" class="headerlink" title="2.1 parse"></a>2.1 parse</h3><p>querystring.parse(str[, sep[, eq[, options]]])</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">const querystring = require(<span class="string">&#x27;querystring&#x27;</span>)</span><br><span class="line">var qs = <span class="string">&#x27;x=3&amp;y=4&#x27;</span></span><br><span class="line">var parsed = querystring.parse(qs)</span><br><span class="line">console.log(parsed)</span><br></pre></td></tr></table></figure>
<h3 id="2-2-stringify"><a href="#2-2-stringify" class="headerlink" title="2.2 stringify"></a>2.2 stringify</h3><p>querystring.stringify(obj[, sep[, eq[, options]]])</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">const querystring = require(<span class="string">&#x27;querystring&#x27;</span>)</span><br><span class="line">var qo = &#123;</span><br><span class="line">  x: 3,</span><br><span class="line">  y: 4</span><br><span class="line">&#125;</span><br><span class="line">var parsed = querystring.stringify(qo)</span><br><span class="line">console.log(parsed)</span><br></pre></td></tr></table></figure>
<h3 id="2-3-escape-unescape"><a href="#2-3-escape-unescape" class="headerlink" title="2.3 escape/unescape"></a>2.3 escape/unescape</h3><p>querystring.escape(str)</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">const querystring = require(<span class="string">&#x27;querystring&#x27;</span>)</span><br><span class="line">var str = <span class="string">&#x27;id=3&amp;city=北京&amp;url=https://www.baidu.com&#x27;</span></span><br><span class="line">var escaped = querystring.escape(str)</span><br><span class="line">console.log(escaped)</span><br></pre></td></tr></table></figure>
<p>querystring.unescape(str)</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">const querystring = require(<span class="string">&#x27;querystring&#x27;</span>)</span><br><span class="line">var str = <span class="string">&#x27;id%3D3%26city%3D%E5%8C%97%E4%BA%AC%26url%3Dhttps%3A%2F%2Fwww.baidu.com&#x27;</span></span><br><span class="line">var unescaped = querystring.unescape(str)</span><br><span class="line">console.log(unescaped)</span><br></pre></td></tr></table></figure>
      
    </div>

  </div>

  <div class="article-footer">
    <div class="article-meta pull-left">

    

    

    </div>

    
  </div>
</article>



  <article>

  
    
    <h3 class="article-title"><a href="/ericxie-book/2021/01/25/list/"><span>list</span></a></h3>
    
  

  <div class="article-top-meta">
    <span class="posted-on">
      <a href="/ericxie-book/2021/01/25/list/" rel="bookmark">
        <time class="entry-date published" datetime="2021-01-25T11:48:17.000Z">
          2021-01-25
        </time>
      </a>
    </span>
  </div>


  

  <div class="article-content">
    <div class="entry">
      
        <h2 id="nodeJs开发"><a href="#nodeJs开发" class="headerlink" title="nodeJs开发"></a>nodeJs开发</h2><h3 id="常用内置模块"><a href="#常用内置模块" class="headerlink" title="常用内置模块"></a>常用内置模块</h3><p>这里介绍几个常用的内置模块：url, querystring, http, events, fs, stream, readline, crypto, zlib</p>
<h4 id="尝试使用浏览器完成调试node代码"><a href="#尝试使用浏览器完成调试node代码" class="headerlink" title="尝试使用浏览器完成调试node代码"></a>尝试使用浏览器完成调试node代码</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">node --inspect a.js</span><br><span class="line">打开地址浏览器地址 chrome://inspect/ </span><br><span class="line">添加地址然后重新打开</span><br><span class="line">node --inspect --inspect-brk  a.js</span><br></pre></td></tr></table></figure>
<h4 id="node进程管理工具"><a href="#node进程管理工具" class="headerlink" title="node进程管理工具"></a>node进程管理工具</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">nodemon</span><br><span class="line">supervisor</span><br><span class="line">forever</span><br><span class="line">pm2</span><br><span class="line"></span><br><span class="line">npm i nodemon -g</span><br><span class="line">nodemon a</span><br><span class="line">或者</span><br><span class="line">npx nodemon a</span><br><span class="line"> </span><br></pre></td></tr></table></figure>
<h2 id="1、nodejs基础"><a href="#1、nodejs基础" class="headerlink" title="1、nodejs基础"></a>1、nodejs基础</h2><h3 id="1、url"><a href="#1、url" class="headerlink" title="1、url"></a>1、<a href="https://xietian79.gitee.io/ericxie-book/2021/01/25/url/index.html">url</a></h3><h3 id="2、querystring"><a href="#2、querystring" class="headerlink" title="2、querystring"></a>2、<a href="https://xietian79.gitee.io/ericxie-book/2021/01/25/querystring/index.html">querystring</a></h3><h3 id="3、http"><a href="#3、http" class="headerlink" title="3、http"></a>3、<a href="https://xietian79.gitee.io/ericxie-book/2021/01/25/http-https/index.html">http</a></h3><h3 id="4、爬虫"><a href="#4、爬虫" class="headerlink" title="4、爬虫"></a>4、<a href="https://xietian79.gitee.io/ericxie-book/2021/01/26/WebCrawlers/index.html">爬虫</a></h3><h3 id="5、事件"><a href="#5、事件" class="headerlink" title="5、事件"></a>5、<a href="https://xietian79.gitee.io/ericxie-book/2021/01/26/event/index.html">事件</a></h3><h3 id="6、路径"><a href="#6、路径" class="headerlink" title="6、路径"></a>6、<a href="https://xietian79.gitee.io/ericxie-book/2021/01/26/path/index.html">路径</a></h3><h3 id="7、file"><a href="#7、file" class="headerlink" title="7、file"></a>7、<a href="https://xietian79.gitee.io/ericxie-book/2021/01/26/file/index.html">file</a></h3><h3 id="8、路由基础"><a href="#8、路由基础" class="headerlink" title="8、路由基础"></a>8、<a href="https://xietian79.gitee.io/ericxie-book/2021/01/26/route/index.html">路由基础</a></h3><h3 id="9、静态资源案例"><a href="#9、静态资源案例" class="headerlink" title="9、静态资源案例"></a>9、<a href="https://xietian79.gitee.io/ericxie-book/2021/01/27/staticPath/index.html">静态资源案例</a></h3><h2 id="2、Yarn入门"><a href="#2、Yarn入门" class="headerlink" title="2、Yarn入门"></a>2、Yarn入门</h2><p><a href="https://xietian79.gitee.io/ericxie-book/2021/01/27/yarn/index.html">传送门</a></p>
<h2 id="3、Express"><a href="#3、Express" class="headerlink" title="3、Express"></a>3、Express</h2><h3 id="1、express基础"><a href="#1、express基础" class="headerlink" title="1、express基础"></a>1、<a href="https://xietian79.gitee.io/ericxie-book/2021/01/27/express-base/index.html">express基础</a></h3><h3 id="2、express中间件和路由"><a href="#2、express中间件和路由" class="headerlink" title="2、express中间件和路由"></a>2、<a href="https://xietian79.gitee.io/ericxie-book/2021/01/27/express-router/index.html">express中间件和路由</a></h3><h2 id="4、art-template"><a href="#4、art-template" class="headerlink" title="4、art-template"></a>4、art-template</h2><p><a href="https://xietian79.gitee.io/ericxie-book/2021/01/30/eart-template/index.html">传送门</a></p>
<h2 id="5、MongoDB数据库"><a href="#5、MongoDB数据库" class="headerlink" title="5、MongoDB数据库"></a>5、MongoDB数据库</h2><h3 id="1、mongodb"><a href="#1、mongodb" class="headerlink" title="1、mongodb"></a>1、<a href="https://xietian79.gitee.io/ericxie-book/2021/01/30/mongoDB/index.html">mongodb</a></h3><h3 id="2、mongoose"><a href="#2、mongoose" class="headerlink" title="2、mongoose"></a>2、<a href="https://xietian79.gitee.io/ericxie-book/2021/01/30/mongoose/index.html">mongoose</a></h3>
      
    </div>

  </div>

  <div class="article-footer">
    <div class="article-meta pull-left">

    

    

    </div>

    
  </div>
</article>



  <article>

  
    
    <h3 class="article-title"><a href="/ericxie-book/2021/01/25/url/"><span>url</span></a></h3>
    
  

  <div class="article-top-meta">
    <span class="posted-on">
      <a href="/ericxie-book/2021/01/25/url/" rel="bookmark">
        <time class="entry-date published" datetime="2021-01-25T07:59:47.000Z">
          2021-01-25
        </time>
      </a>
    </span>
  </div>


  

  <div class="article-content">
    <div class="entry">
      
        <h2 id="1、url"><a href="#1、url" class="headerlink" title="1、url"></a>1、url</h2><p>新建URL对象</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">const url=require(<span class="string">&quot;url&quot;</span>);</span><br><span class="line">var myUrl=new URL(<span class="string">&quot;http://10.9.63.202:3005?name=xie&quot;</span>);</span><br></pre></td></tr></table></figure>
<h3 id="1-1-parse"><a href="#1-1-parse" class="headerlink" title="1.1 parse"></a>1.1 parse</h3><p>url.parse(urlString[, parseQueryString[, slashesDenoteHost]])</p>
<img src="/ericxie-book/2021/01/25/url/url.jpg" class="" title="url图片">

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">const url = require(<span class="string">&#x27;url&#x27;</span>)</span><br><span class="line">const urlString = <span class="string">&#x27;https://www.baidu.com:443/ad/index.html?id=8&amp;name=mouse#tag=110&#x27;</span></span><br><span class="line">const parsedStr = url.parse(urlString)</span><br><span class="line">console.log(parsedStr)</span><br></pre></td></tr></table></figure>
<h3 id="1-2-format"><a href="#1-2-format" class="headerlink" title="1.2 format"></a>1.2 format</h3><p>url.format(urlObject)</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">const url = require(<span class="string">&#x27;url&#x27;</span>)</span><br><span class="line">const urlObject = &#123;</span><br><span class="line">    protocol: <span class="string">&#x27;https:&#x27;</span>,</span><br><span class="line">    slashes: <span class="literal">true</span>,</span><br><span class="line">    auth: null,</span><br><span class="line">    host: <span class="string">&#x27;www.baidu.com:443&#x27;</span>,</span><br><span class="line">    port: <span class="string">&#x27;443&#x27;</span>,</span><br><span class="line">    hostname: <span class="string">&#x27;www.baidu.com&#x27;</span>,</span><br><span class="line">    <span class="built_in">hash</span>: <span class="string">&#x27;#tag=110&#x27;</span>,</span><br><span class="line">    search: <span class="string">&#x27;?id=8&amp;name=mouse&#x27;</span>,</span><br><span class="line">    query: &#123; </span><br><span class="line">        id: <span class="string">&#x27;8&#x27;</span>, name: <span class="string">&#x27;mouse&#x27;</span> </span><br><span class="line">        &#125;,</span><br><span class="line">    pathname: <span class="string">&#x27;/ad/index.html&#x27;</span>,</span><br><span class="line">    path: <span class="string">&#x27;/ad/index.html?id=8&amp;name=mouse&#x27;</span>,</span><br><span class="line">    href: <span class="string">&#x27;https://www.baidu.com:443/ad/index.html?id=8&amp;name=mouse#tag=110&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">const parsedObj = url.format(urlObject)</span><br><span class="line">console.log(parsedObj)</span><br></pre></td></tr></table></figure>
<h3 id="1-3-resolve"><a href="#1-3-resolve" class="headerlink" title="1.3 resolve"></a>1.3 resolve</h3><p>url.resolve(from, to)</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">const url = require(<span class="string">&#x27;url&#x27;</span>)</span><br><span class="line">var a = url.resolve(<span class="string">&#x27;/one/two/three&#x27;</span>, <span class="string">&#x27;four&#x27;</span>)</span><br><span class="line">var b = url.resolve(<span class="string">&#x27;http://example.com/&#x27;</span>, <span class="string">&#x27;/one&#x27;</span>)</span><br><span class="line">var c = url.resolve(<span class="string">&#x27;http://example.com/one&#x27;</span>, <span class="string">&#x27;/two&#x27;</span>)</span><br><span class="line">console.log(a + <span class="string">&quot;,&quot;</span> + b + <span class="string">&quot;,&quot;</span> + c)</span><br></pre></td></tr></table></figure>
<h3 id="1-4-log4js插件"><a href="#1-4-log4js插件" class="headerlink" title="1.4 log4js插件"></a>1.4 log4js插件</h3><p>log4js是一个日志处理文件</p>
<p>安装</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i log4js -D</span><br></pre></td></tr></table></figure>
<p>使用方式</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">const log4js = require(<span class="string">&quot;log4js&quot;</span>);</span><br><span class="line">log4js.configure(&#123;</span><br><span class="line">  appenders: &#123; cheese: &#123; <span class="built_in">type</span>: <span class="string">&quot;file&quot;</span>, filename: <span class="string">&quot;cheese.log&quot;</span> &#125; &#125;,</span><br><span class="line">  categories: &#123; default: &#123; appenders: [<span class="string">&quot;cheese&quot;</span>], level: <span class="string">&quot;error&quot;</span> &#125; &#125;</span><br><span class="line">&#125;);</span><br><span class="line">const logger = log4js.getLogger(<span class="string">&quot;cheese&quot;</span>);</span><br><span class="line">logger.level=<span class="string">&quot;debug&quot;</span>;</span><br><span class="line"></span><br><span class="line">var str=<span class="string">&quot;http://www.163.com/news?a=3&quot;</span>;</span><br><span class="line">logger.debug(url.parse(str));</span><br></pre></td></tr></table></figure>
<h3 id="1-5-URLSearchParams"><a href="#1-5-URLSearchParams" class="headerlink" title="1.5 URLSearchParams"></a>1.5 URLSearchParams</h3><p>该类是用来搜索地址中的参数，并且获取到指定的参数值</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">var str=<span class="string">&quot;http://www.163.com/news/?a=3&amp;b=4&quot;</span>;</span><br><span class="line">var search=url.parse(str).search;</span><br><span class="line">var urlParams=new URLSearchParams(search);</span><br><span class="line">var a=urlParams.get(<span class="string">&quot;a&quot;</span>);</span><br><span class="line">var b=urlParams.get(<span class="string">&quot;b&quot;</span>);</span><br></pre></td></tr></table></figure>
<h3 id="Crypto"><a href="#Crypto" class="headerlink" title="Crypto"></a>Crypto</h3><p>加密</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">const crypto = require(<span class="string">&#x27;crypto&#x27;</span>)</span><br><span class="line">const secret = <span class="string">&#x27;abcdefg&#x27;</span></span><br><span class="line">const <span class="built_in">hash</span> = crypto.createHmac(<span class="string">&#x27;sha256&#x27;</span>, secret)</span><br><span class="line">                   .update(<span class="string">&#x27;I love you&#x27;</span>)</span><br><span class="line">                   .digest(<span class="string">&#x27;hex&#x27;</span>)</span><br><span class="line">console.log(<span class="built_in">hash</span>);</span><br></pre></td></tr></table></figure>
      
    </div>

  </div>

  <div class="article-footer">
    <div class="article-meta pull-left">

    

    

    </div>

    
  </div>
</article>



  <article>

  
    
    <h3 class="article-title"><a href="/ericxie-book/2021/01/25/hello-world/"><span>Hello World</span></a></h3>
    
  

  <div class="article-top-meta">
    <span class="posted-on">
      <a href="/ericxie-book/2021/01/25/hello-world/" rel="bookmark">
        <time class="entry-date published" datetime="2021-01-25T07:10:29.883Z">
          2021-01-25
        </time>
      </a>
    </span>
  </div>


  

  <div class="article-content">
    <div class="entry">
      
        <p>Welcome to <a target="_blank" rel="noopener" href="https://hexo.io/">Hexo</a>! This is your very first post. Check <a target="_blank" rel="noopener" href="https://hexo.io/docs/">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a target="_blank" rel="noopener" href="https://hexo.io/docs/troubleshooting.html">troubleshooting</a> or you can ask me on <a target="_blank" rel="noopener" href="https://github.com/hexojs/hexo/issues">GitHub</a>.</p>
<h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo new <span class="string">&quot;My New Post&quot;</span></span><br></pre></td></tr></table></figure>
<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/writing.html">Writing</a></p>
<h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></table></figure>
<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/server.html">Server</a></p>
<h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></table></figure>
<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/generating.html">Generating</a></p>
<h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></table></figure>
<p>More info: <a target="_blank" rel="noopener" href="https://hexo.io/docs/one-command-deployment.html">Deployment</a></p>

      
    </div>

  </div>

  <div class="article-footer">
    <div class="article-meta pull-left">

    

    

    </div>

    
  </div>
</article>




<nav class="pagination">
  
  <a href="/ericxie-book/" class="pagination-prev">Prev</a>
  
  
</nav>
    </main>

    <footer class="site-footer">
  <p class="site-info">
    Proudly powered by <a href="https://hexo.io/" target="_blank">Hexo</a> and
    Theme by <a href="https://github.com/CodeDaraW/Hacker" target="_blank">Hacker</a>
    </br>
    
    &copy; 2021 John Doe
    
  </p>
</footer>
    
    
  </div>
</div>
</body>
</html>